package com.guigu.servicegoods.controller;


import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicegoods.domain.TbBrand;
import com.guigu.servicegoods.service.TbBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/servicegoods/brand")
public class TbBrandController {

    @Autowired
    private TbBrandService brandService;

    //查询全部
    @GetMapping("/findAll")
    public Result findAll(){
        List<TbBrand> brandList = brandService.list(null);
        return Result.ok().data("brandList",brandList);
    }

    // 分页条件查询
    @PostMapping("/findPage")
    public Result findPage(@RequestBody(required = false) Map<String, Object> searchMap,Integer page,Integer size) {
        return brandService.findPage(searchMap, page, size);
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody TbBrand brand) {
        boolean save = brandService.save(brand);
        if (save){
            return Result.ok();
        }
        return Result.error();
    }

    @PostMapping("/update")
    public Result update(@RequestBody TbBrand brand) {
        boolean update = brandService.updateById(brand);
        if (update) {
            return Result.ok();
        }
        return Result.error();
    }

    @GetMapping("/delete")
    public Result delete(Integer id) {
        boolean remove = brandService.removeById(id);
        if (remove){
            return Result.ok();
        }
        return Result.error();
    }

    @GetMapping("/findById")
    public Result findById(Integer id) {
        TbBrand brand = brandService.getById(id);
        if (brand==null){
            return Result.error();
        }
        return Result.ok().data("brand",brand);
    }
}

